package com.example.demo.controller.front;

import com.example.demo.dao.Comment;
import com.example.demo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    //查找评论
    @RequestMapping("/find")
    public String findcom(Model model){
        List<Comment> comments = commentService.findAll();
        model.addAttribute("comment",comments);
        return "show";
    }

    //添加评论
    @PostMapping(value = "/add")
    public String addcomment(HttpSession session,String comment){
        Integer userid =(Integer) session.getAttribute("id");
        System.out.println(userid);
        Integer goodsid=(Integer)session.getAttribute("goodsid");
        //时间
        Date time = new Date();
        java.sql.Date date = new java.sql.Date(time.getTime());
        Comment commentl=new Comment(1,userid,goodsid,1,comment,date);
        commentService.addcomment(commentl);
        return "redirect:/goods/find?id="+goodsid;
    }

}
